//
//  EpointExtendAppGridViewController.m
//  EpointFrame5
//
//  Created by wangzhigang on 14-5-21.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointExtendAppGridViewController.h"
#import "EAD_AppExtendView.h"
#import "EpointLoadMainConfigTask.h"
#import "EpointMainConfigModel.h"
#import "HJObjManager.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

@interface EpointExtendAppGridViewController ()
{
    //列数
    int colNums;
    
    //列宽
    CGFloat colWidth;
    
    //行宽
    CGFloat rowHeight;
    
    NSMutableArray *arr;
    
    int iconNums;
    
    HJObjManager *objMan;
    
    UIScrollView *sv;
}
@end

@implementation EpointExtendAppGridViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    sv = [[UIScrollView alloc]initWithFrame:self.getFullContentFrame];
    [self.view addSubview:sv];
    colNums = CurrentDeviceISiPad?8:4;
    
    colWidth = AppWidth/colNums;
    
    rowHeight = 100;
    
    iconNums = 9;
    
    [self initHJManage];

    arr = [EpointLoadMainConfigTask getExtendAppByStore];
    
    for (int i=0; i<arr.count; i++) {
        
        EpointMainConfigModel *m = [arr objectAtIndex:i];
        
        EAD_AppExtendView *view = [[EAD_AppExtendView alloc]initWithFrame:CGRectMake((i%colNums)*colWidth, (i/colNums)*rowHeight, colWidth, rowHeight)];
        view.ivIcon.url = [NSURL URLWithString:[EpointBizTools getUrl:[NSString stringWithFormat:@"mobileconfig/mainimg/%@",m.iconUrl]]];
        [objMan manage:view.ivIcon];
        view.lbTitle.text = m.modulename;
        
        view.handler = ^{
            if (![[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:m.launchclass]]) {
                RIButtonItem *okItem = [RIButtonItem item];
                okItem.label = @"确定";
                okItem.action = ^{
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:m.apkUrl]];
                };
                
                RIButtonItem *cancelItem = [RIButtonItem item];
                cancelItem.label = @"取消";
                
                NSString *message = [NSString stringWithFormat:@"尚未安装%@，需要下载并安装吗？",m.modulename];
                
                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:message cancelButtonItem:cancelItem otherButtonItems:okItem,nil];
                
                [alter show];
            }
            else
            {//m.launchclass+"//{"loginid":"liyc","psw":"123456"}"
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:m.launchclass]];
            }
        };
        
        if (i==99) {
            [sv setContentSize:CGSizeMake(AppWidth, view.frame.size.height+view.frame.origin.y)];
        }
        [sv addSubview:view];
    }
    
    
    
    // Do any additional setup after loading the view.
}

//初始化图片缓存管理器
-(void)initHJManage
{
    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:20 memCacheSize:30];
    NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/flickr/"] ;
    HJMOFileCache* fileCache = [[HJMOFileCache alloc] initWithRootPath:cacheDirectory];
    objMan.fileCache = fileCache;
    fileCache.fileCountLimit = 100;
    fileCache.fileAgeLimit = 60*60*24*30; //1 week
    [fileCache trimCacheUsingBackgroundThread];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
